package com.budweiser.user.controller;

import com.budweiser.commons.utils.Utils;
import com.budweiser.model.log.LogAnnotation;
import com.budweiser.model.page.Page;
import com.budweiser.model.result.ResponseBase;
import com.budweiser.model.user.Permission;
import com.budweiser.user.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
public class PermissionController {

	@Autowired
	private PermissionService permissionService;

	/**
	 * 管理后台添加权限
	 * 
	 * @param
	 * @return
	 */
	@LogAnnotation(module = "添加权限")
	@PreAuthorize("hasAuthority('back:permission:save')")
	@PostMapping("/permissions")
	public ResponseBase save(Permission permission) {
		permissionService.save(permission);
		return new ResponseBase(200, "新增成功！", null);
	}

	/**
	 * 管理后台修改权限
	 * 
	 * @param
	 */
	@LogAnnotation(module = "修改权限")
	@PreAuthorize("hasAuthority('back:permission:update')")
	@PutMapping("/permissions")
	public ResponseBase update(@RequestBody Permission permission) {
		permissionService.update(permission);
		return new ResponseBase(200, "修改成功！", null);
	}

	/**
	 * 删除权限标识
	 * 
	 * @param
	 */
	@LogAnnotation(module = "删除权限")
	@PreAuthorize("hasAuthority('back:permission:delete')")
	@DeleteMapping("/permissions/{id}")
	public ResponseBase delete(String ids) {
		permissionService.delete(Utils.getLongs(ids));
		return new ResponseBase(200, "删除成功！", null);
	}

	/**
	 * 查询所有的权限标识
	 */
	@PreAuthorize("hasAuthority('back:permission:query')")
	@GetMapping("/permissions")
	public Page<Permission> findPermissions(@RequestParam Map<String, Object> params) {
		return permissionService.findPermissions(params);
	}

	@GetMapping("/permission/all")
	public Page<Permission> getPermissions(@RequestParam Map<String, Object> params, String page, String limit) {
		params.put("start", page);
		params.put("length", limit);
		return permissionService.findPermissions(params);
	}

}
